package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.ExamsInfo;

import com.fsc.entity.vo.TotalScoreInfoVO;
import com.fsc.service.ExamsInfoService;
import com.fsc.service.StuPartTeachService;
import com.fsc.service.TotalScoreInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/totalScore")
public class TotalScoreController {

	@Autowired
	private TotalScoreInfoService totalScoreInfoService;
	@Autowired
	private StuPartTeachService stuPartTeachService;

	@GetMapping("/exam/{examsId}")
	public ResponseEntity<List<TotalScoreInfoVO>> getTotalScoreByExamId(@PathVariable int examsId) {
		List<TotalScoreInfoVO> totalScores = totalScoreInfoService.getTotalScoreByExamId(examsId);
		return ResponseEntity.ok(totalScores);
	}

	/*
	学生根据某次成绩查看自己的各项成绩
	 */
	@GetMapping("/student/{examsId}")
	public ResponseEntity <TotalScoreInfoVO> getTotalScoreByStudentId(@PathVariable int examsId) {
		Integer studentId = Integer.parseInt((String) StpUtil.getLoginId());
		TotalScoreInfoVO totalScores = totalScoreInfoService.getTotalScoreByStudentId(examsId, studentId);
		System.out.println(totalScores);
		return ResponseEntity.ok(totalScores);
	}


	@GetMapping("/par")
	public ResultWrapper<List<TotalScoreInfoVO>> getStuByParId(int parentId, int examId){

		List<Integer> ids = stuPartTeachService.getStudentByParentId(parentId);
		for (int i = 0; i < ids.size(); i++) {
			System.out.println(ids.get(i));
		}
		List<TotalScoreInfoVO> voList = totalScoreInfoService.getTotalScoreByStuIds(examId, ids);
		for (int i = 0; i < voList.size(); i++) {
			System.out.println(voList.get(i));
		}
		return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
	}

	/**
	 * 老师导入成绩
	 */
	@PostMapping("/import/{examsId}")
	public ResultWrapper importScore(
			@PathVariable("examsId") Integer examsId,
			@RequestBody List<TotalScoreInfoVO> totalScoreInfoVOList) {
		totalScoreInfoService.importScore(totalScoreInfoVOList, examsId);
		return ResultWrapper.success(ReturnEnum.SUCCESS);
	}

	/**
	 * 根据id删除某个成绩
	 */
	@DeleteMapping("/delete/{id}")
	public ResultWrapper deleteScore(@PathVariable int id) {
		totalScoreInfoService.removeById(id);
		return ResultWrapper.success(ReturnEnum.SUCCESS);
	}

	/**
	 * 根据id更新某个成绩
	 */
	@PutMapping("/update")
	public ResultWrapper updateScore(@RequestBody TotalScoreInfoVO totalScoreInfoVO) {
		totalScoreInfoService.updateById(totalScoreInfoVO);
		return ResultWrapper.success(ReturnEnum.SUCCESS);
	}

}
